﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class stateCityManager : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            CSUtils.CheckSession();
            this.binddataState();
        }
    }

    private void binddataState()
    {
        DropDownListState.DataSource = CSUtils.getAllState();
        DropDownListState.DataBind();
    }
     

    protected void Button1_Click(object sender, EventArgs e) {
        this.binddata();
        PanelInput.Visible = false;
        LabelState.Text = "";
    }

    private void binddata() {
        string stateID = DropDownListState.SelectedValue;
        GridView1.DataSource = this.getAllCity(stateID);
        GridView1.DataBind();

    }


    private DataSet getAllCity(string stateID)
    {
        string sql =
            "select * from cn_city where stateID = " + stateID + " order by cityID";        
        
        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        GridView1.PageIndex = e.NewPageIndex;
        this.binddata();
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {

        string cityID = e.CommandArgument.ToString();
        string stateID = DropDownListState.SelectedValue;

        if (e.CommandName.Equals("command_del"))
        {
            if (this.delCity(stateID, cityID))
            {
            }
            else
            {
                JScript.Alert("删除失败！");
            }

            this.binddata();

        }
        else if (e.CommandName.Equals("command_edit"))
        {
            PanelInput.Visible = true;
            LabelState.Text = DropDownListState.SelectedItem.Text;
            LabelCityID.Text = cityID;
            TextBoxCitytName.Text = this.getCityName(DropDownListState.SelectedValue, cityID);
        }
        else
        {
            
        }
    }

    protected void LinkButtonAdd_Click(object sender, EventArgs e)
    {
        PanelInput.Visible = true;
        LabelState.Text = DropDownListState.SelectedItem.Text;
        LabelCityID.Text = "";
        TextBoxCitytName.Text = "";

    }



    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {

        string cityID = LabelCityID.Text;
        string cityName = TextBoxCitytName.Text;
        string stateID = DropDownListState.SelectedValue;

        if (this.mergeCity(stateID, cityID, cityName))
        {
        }
        else
        {
            JScript.Alert("保存失败！");
        }

        this.binddata();
    }

    private string getCityName(string stateID, string cityID)
    {
        string cityName = "";

        string sql =
            "select cityName from cn_city where stateID = " + stateID + " and cityID = " + cityID;

        DBConn myDB = new DBConn();
        SqlDataReader dr = myDB.getDataReader(sql);
        if (dr.Read())
        {
            cityName = dr["cityName"].ToString();
        }
        dr.Close();
        myDB.Close();

        return cityName;
    }

    private bool mergeCity(string stateID, string cityID, string cityName)
    {

        string sqlInsert =
            "insert into cn_city(cityID, cityName, stateID) " +
            "VALUES({0}, '{1}', {2})";

        string sqlUpdate =
            "update cn_city set cityName = '{0}' where cityID = {1} and stateID = {2} ";
            

        DBConn myDB = new DBConn();
        int tmp = 0;
        if (cityID == "")
        {
            cityID = this.getMaxCityID(stateID);
            sqlInsert = string.Format(sqlInsert, cityID, cityName, stateID);
            tmp = myDB.Insert(sqlInsert);
        }
        else
        {
            sqlUpdate = string.Format(sqlUpdate, cityName, cityID, stateID);
            tmp = myDB.Update(sqlUpdate);
        }
        myDB.Close();
        return (tmp == 1);

    }

    private string getMaxCityID(string stateID)
    {
        string maxNumber = "";
        string sql =
            "select max(cityID)+1 as maxNumber from cn_city where stateID = " + stateID;

        DBConn myDB = new DBConn();
        SqlDataReader dr = myDB.getDataReader(sql);
        if (dr.Read())
        {
            maxNumber = dr["maxNumber"].ToString();
        }
        dr.Close();
        myDB.Close();        

        return maxNumber;

    }

    private bool delCity(string stateID, string cityID)
    {
        string sql =
            "delete from cn_city where stateID = " + stateID + " and cityID = " + cityID;

        DBConn myDB = new DBConn();
        int ret = myDB.Update(sql);
        myDB.Close();
        return ret > 0;
    }

}
